//
//  FundView.m
//  Project two framework
//
//  Created by 张峥 on 16/7/25.
//  Copyright © 2016年 张峥. All rights reserved.
//

#import "FundView.h"
#import "FundCollectionViewCell.h"
#import "ZPZNetRequest+FundDetail.h"
#import "MJRefresh.h"
#import "ManageView.h"
#import "SingleView.h"

@interface FundView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UIScrollView          *scrollView;
@property (nonatomic, strong) UICollectionView      *collectView;
@property (nonatomic, strong) NSMutableArray        *dataSource;

@end
@implementation FundView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self stepUI];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(infobtntap) name:@"gz" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(buyBtnTap) name:@"zg" object:nil];
    }
    return self;
}

- (void)loadDataSource{
    [ZPZNetRequest getFundListPageNum:@"1" pageSize:@"10" userId:@"22" comletion:^(id object, BOOL success) {
        if (success) {
            _dataSource = [NSMutableArray arrayWithArray:object[@"content"]];
            [_collectView reloadData];
        }
    }];
}

//刷新方法
- (void)refreshHomeHeader{
    [ZPZNetRequest getFundListPageNum:@"1" pageSize:@"10" userId:@"22" comletion:^(id object, BOOL success) {
        if (success) {
            _dataSource = [NSMutableArray arrayWithArray:object[@"content"]];
            [_collectView reloadData];
            [_scrollView.mj_header endRefreshing];

        }
    }];
}

- (void)stepUI{
    [self addSubview:self.scrollView];
    [self.scrollView addSubview:self.collectView];
    [self loadDataSource];
}

#pragma mark   ---  自定义方法
- (NSString *)numberWithString:(NSNumber *)number{
    NSString *string = [NSString stringWithFormat:@"%@",number];
    return string;
}


//通知回调
- (void)infobtntap{
         ManageView *mangeView = [ManageView showOtherView];
    NSString *string1 = [NSString stringWithFormat:@"          基金代码:%@",_dataSource[0][@"product_num"]];
    NSString *string2 = [NSString stringWithFormat:@"          基金简称:%@",_dataSource[0][@"name"]];
    NSString *string3 = [NSString stringWithFormat:@"          单位净值:%@",[self numberWithString:_dataSource[0][@"asset_value"]]];
    NSString *string4 = [NSString stringWithFormat:@"          累计净值:%@",[self numberWithString:_dataSource[0][@"accumulative_value"]]];
    NSString *string5 = [NSString stringWithFormat:@"          截止日期:%@",_dataSource[0][@"stop_time"]];


            mangeView.nameLabel.text = _dataSource[0][@"name"];
            mangeView.fundLabel.text = string1;
            mangeView.abbreviationLabel.text = string2;
            mangeView.unitLabel.text = string3;
            mangeView.accumulativeLabel.text = string4;
            mangeView.deadlineLabel.text = string5;
}

- (void)buyBtnTap{
    [SingleView showOtherView];
}


- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.contentSize = CGSizeMake(0, self.bounds.size.height*1.5);
        _scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self refreshHomeHeader];
        }];
    }
    return _scrollView;
}


- (UICollectionView *)collectView{
    if (!_collectView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(275, 275);
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 20;
        
        //设置滚动方向
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), 300) collectionViewLayout:layout];
        _collectView.backgroundColor = [UIColor whiteColor];
        _collectView.showsHorizontalScrollIndicator = NO;
        _collectView.delegate = self;
        _collectView.contentInset = UIEdgeInsetsMake(10, 20, 0, 0);
        _collectView.pagingEnabled = YES;
        _collectView.dataSource = self;
        [_collectView registerNib:[UINib nibWithNibName:@"FundCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"FundView"];
    }
    return _collectView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    FundCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FundView" forIndexPath:indexPath];
    NSDictionary *dic = _dataSource[indexPath.row];
    cell.titleLabel.text = [NSString stringWithFormat:@"【%@】",dic[@"number"]];
    cell.mainLabel.text = dic[@"name"];
    cell.unitLabel.text = [NSString stringWithFormat:@"单位净值:%@",dic[@"asset_value"]];
    cell.totalLabel.text = [NSString stringWithFormat:@"累计净值:%@",dic[@"accumulative_value"]];
    cell.timeLabel.text = [NSString stringWithFormat:@"截止日期:%@",dic[@"stop_time"]];
    return cell;
}



@end
